// page/addaddress/addaddress.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        addressId:0,
        // customItem: '全部',
        region: [],
        receiver: '张三',
        phone: '15215485695',
        area: 'XXX街道',
        city: ''
    },

/////

    input(e){
        let data = {}
        data[e.currentTarget.dataset.key] = e.detail.value
        this.setData(data)
    },
//////地址选择
    bindPickerChange: function(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
          index: e.detail.value
        })
      },

    bindRegionChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
          region: e.detail.value
        })
    },
    /////提交保存按钮

    submit(){

        let address = {
            memberId:wx.userInfo._id,
            receiver: this.data.receiver,
            phone: this.data.phone,
            area: this.data.area,
            city: this.data.region.join('')
        }
        /////校验规则
        // /[^\u4E00-\u9FA5]/     /^[\u2E80-\u9FFF] + $/
        if(!address.receiver || !/^[\u2E80-\u9FFF]+$/.test(address.receiver)){
            wx.showToast({
              title: '请输入正确的收货人',
              duration: 2000, 
              icon:'none'
            })
            return false
        }
        if(!address.phone || !/^1\d{10}$/.test(address.phone)){
            wx.showToast({
              title: '请输入正确的号码',
              duration: 2000, 
              icon:'none'
            })
            return false
        }
        if(!address.city && !this.data.addressId){
            wx.showToast({
                title: '请选择地址',
                duration: 2000, 
                icon:'none'
              })
              return false
        }
        if(this.data.addressId){////修改
            address.addressId = this.data.addressId
            wx.put('http://localhost:3000/wx/address',address).then(res =>{
                if(!res.errorMsg){
                    wx.showToast({
                        title: '修改成功',
                        duration: 2000, 
                        icon:'success'
                    })
                    setTimeout(()=> {
                        wx.navigateBack({
                            delta: -1
                        })
                    },2000)
                }else{
                    wx.showToast({
                        title: res.errorMsg,
                        duration: 2000, 
                        icon:'fail'
                    })
                }
            })
        }else{//新增
            wx.post('http://localhost:3000/wx/address',address).then(res =>{
                if(!res.errorMsg){
                    wx.showToast({
                        title: '新建成功',
                        duration: 2000, 
                        icon:'success'
                    })
                    setTimeout(()=> {
                        wx.navigateBack({
                            delta: -1
                        })
                    },2000)
                }else{
                    wx.showToast({
                        title: res.errorMsg,
                        duration: 2000, 
                        icon:'fail'
                    })
                }
            })
        }

    },
    getAddressDetail(){
        wx.get('http://localhost:3000/wx/address/detail',{
            addressId: this.data.addressId
        }).then(res => {
            this.setData({
                receiver:res.receiver,
                phone: res.phone,
                city: res.city,
                area: res.area
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if(options.addressId){
            this.setData({
                addressId: options.addressId
            })
            /////获取地址详情
            this.getAddressDetail()
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})